package com.binyouwei.designpatterns.creation_type.singleton;

/**
 * @author 宾有为
 * @date 2024/6/20 20:13
 * @link https://blog.csdn.net/baidu_41616022?type=blog
 * @describe：单例模式 —— 双重检查锁定
 * 优点：线程安全，比单纯的懒汉式加锁有更高的性能。
 * 缺点：在某些版本的Java中，可能会有指令重排序问题，需要加volatile关键字。
 **/
public class DoubleCheckedLocking {
    private static volatile DoubleCheckedLocking instance;

    private DoubleCheckedLocking() {}

    public static DoubleCheckedLocking getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckedLocking.class) {
                if (instance == null) {
                    instance = new DoubleCheckedLocking();
                }
            }
        }
        return instance;
    }

    public void print(){
        System.out.println("Double Checked Locking");
    }
}
